import { APlugin } from '../types/common.interface';
import { TableDatas } from '../cors/table-datas';
import { Inject, Injectable, PluginInjectable } from '../decorators/inject';
import { Select } from '../cors/select';
import { ExecuteRecord } from '../cors/execute-record';
import { Hook } from '../cors/hook';
import { RowHeightCommand } from '../commands/row-height-command';
import { NExcel } from '../types';
import { Config } from '../cors/config';
import { createElement } from '../utils/dom-helper';
import { fromEvent } from 'rxjs';
import { filter } from 'rxjs/operators';
import './row-height.plugin.less';
import { EventHelper } from '../utils/event-helper';
import { RenderTrigger } from '../cors/render-trigger';
import { Cache } from '../cors/cache';
import { ScaleCommand } from '../commands/scale-command';

@Injectable()
@PluginInjectable('ScalePlugin')
export class ScalePlugin  extends APlugin{
    constructor(
        protected conf: Config,
        protected render: RenderTrigger,
        protected executeRecord: ExecuteRecord
    ) {
        super();
    }
    public getCurrentScale() {
        return this.conf.scale;
    }
    public begin() {
        let scale = this.conf.scale;
        let latestScale = -1;
        let isCommited = false;
        return {
            setScale: (val: number) => {
                if(isCommited) {
                    console.warn('已经提交，不能修改了');
                    return;
                }
                this.conf.updateScale(val);
                latestScale = val;
                this.render.triggerRenderAll();
            },
            rollback: () => {
                if(isCommited) {
                    console.warn('已经提交，不能回滚了');
                    return;
                } else {
                    isCommited = true;
                }
                if(latestScale === -1) {
                    return;
                }
                this.conf.updateScale(scale);
                this.render.triggerRenderAll();
            },
            commit: () => {
                if(isCommited) {
                    console.warn('已经提交，不能重复提交');
                    return;
                } else {
                    isCommited = true;
                }
                this.conf.scale = scale;
                let command = new ScaleCommand(this.conf,this.render,latestScale);
                this.executeRecord.execute(command);
            }
        }
    }
    public setScale(scale: number) {
        let command = new ScaleCommand(this.conf,this.render,scale);
        this.executeRecord.execute(command);
    }
    public init(): void {
        
    }
    public destroy(): void {
        
    }
}